package zh.dummy.netty.server.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.ReadTimeoutHandler;
import zh.dummy.netty.core.codec.NettyMessageDecoder;
import zh.dummy.netty.core.codec.NettyMessageEncoder;

public class DefaultChannelHandler extends ChannelInitializer<SocketChannel>{

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		//cut message length field
		ch.pipeline().addLast(
				new LengthFieldBasedFrameDecoder(1024*1024, 0, 4,0,4));
		//byte to message
		ch.pipeline().addLast(
				new NettyMessageDecoder());
		ch.pipeline().addLast(
				new ReadTimeoutHandler(50));
		//prepend message length field
		ch.pipeline().addLast(
				new LengthFieldPrepender(4,0,false));
		//messgae to byte
		ch.pipeline().addLast(
				new NettyMessageEncoder());
		//login response
		ch.pipeline().addLast(
				new LoginAuthRespHandler());
		//heartbeat response
		ch.pipeline().addLast(
				new HeartBeatRespHandler());
		ch.pipeline().addLast(
				new WorkflowHandler());
		ch.pipeline().addLast(new FinalChannelHandler());
	}
	
}
